<?php

namespace app\controller\api;

use app\BaseController;
use app\constant\CommonConstant;
use app\constant\ModelConstant;
use app\helpers\UtilHelper;
use app\model\PixelLogs;
use app\queue\PixelTrackQueue;
use Carbon\Carbon;
use think\facade\Queue;
use think\helper\Arr;
use think\Request;

class FaceBookPixelController extends BaseController
{
    public function track_event_by_client(Request $request)
    {

        if(empty($request->post())){
            return json(['msg'=>"参数错误",'code'=>400,'data'=>[]],400);
        }
        $event_type = $request->post('event_type');
        $ip = getClientIP();
        $product_id = $request->post('product_id', '0');
        $order = $request->param('order', []);
        $orderId = Arr::get($order, 'id', 0);
        $referer = $request->header('referer', '');
        $refererList = explode('?', $referer);
        $queryStr = Arr::last($refererList);
        parse_str($queryStr, $urlParams);
        $platform = CommonConstant::TRAFFIC_SOURCE_FB;
        $fbclid = $request->post('fbclid', '');
        $ttclid = $request->post('ttclid', '');
        $shop_code = UtilHelper::absPath(Arr::first($refererList));
        if ($ttclid) {
            $platform = CommonConstant::TRAFFIC_SOURCE_TK;
            $fbclid = $ttclid;
        }
        $data = [
            'ip' => $ip,//获取用户IP地址
            'event_type' => $event_type,
            'fbclid' => $fbclid,
            'user_agent' => $request->header('User-Agent'),
            'website' => $referer,
            'params' => $request->param(),
            "fbc_at" => round(microtime(true) * 1000),
            'response' => [],
            'product_id' => $product_id,
            'order_id' => $orderId,
            'platform' => $platform,
            'shop_code' => $shop_code
        ];
        //dd($data);
        if ($platform == CommonConstant::TRAFFIC_SOURCE_TK) {
            $log = PixelLogs::createTrackLog($data);
            return $this->success($log ? $log->id : 0);
        } else {
            Queue::push(PixelTrackQueue::class, $data, "TrackQueue");
            return $this->success();
        }
    }

    public function track_event(Request $request)
    {
       tplog("TrackPixelLog: ", $request->post(),'pixel');
        if(empty($request->post())){
            return json(['msg'=>"参数错误",'code'=>400,'data'=>[]]);
        }
        $event_type = $request->post('event_type',CommonConstant::FB_EVENT_PREVIEW);
        $ip = $request->post('ip','127.0.0.1');
        $product_id = $request->post('product_id', '0');
        $order = $request->param('order', []);
        $orderId = Arr::get($order, 'id', 0);
        $website = $request->post('website', '');
        $platform = CommonConstant::TRAFFIC_SOURCE_FB;
        $fbclid = $request->post('fbclid', '');
        $ttclid = $request->post('ttclid', '');
        $shop_code = $request->post("shop_code",'');
        $amount = Arr::get($order,"amount",0);
        if(!$orderId || !$amount) return json(['msg'=>"参数错误",'code'=>400,'data'=>[]]);
        if ($ttclid) {
            $platform = CommonConstant::TRAFFIC_SOURCE_TK;
            $fbclid = $ttclid;
        }
        $data = [
            'ip' => $ip,//获取用户IP地址
            'event_type' => $event_type,
            'fbclid' => $fbclid,
            'user_agent' => $request->post("user_agent",''),
            'website' => $website,
            'params' => $request->param(),
            "fbc_at" => round(microtime(true) * 1000),
            'response' => [],
            'product_id' => $product_id,
            'order_id' => $orderId,
            'platform' => $platform,
            'shop_code' => $shop_code
        ];
        //dd($data);
        if (env('APP_ENV') == "local") {
            $log = PixelLogs::createTrackLog($data);
            return $this->success($log ? $log->id : 0);
        } else {
            Queue::push(PixelTrackQueue::class, $data, "TrackQueue");
            return $this->success();
        }
    }
}